package com.hssy.wululuaplan.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hssy.wululuaplan.entity.Course;
import com.hssy.wululuaplan.entity.CourseSchedule;
import com.hssy.wululuaplan.entity.DivideClass;
import com.hssy.wululuaplan.exception.CustomException;
import com.hssy.wululuaplan.form.CourseForm;
import com.hssy.wululuaplan.mapper.AttendanceMapper;
import com.hssy.wululuaplan.mapper.CourseMapper;
import com.hssy.wululuaplan.mapper.CourseScheduleMapper;
import com.hssy.wululuaplan.mapper.DivideClassMapper;
import com.hssy.wululuaplan.service.AttendanceService;
import com.hssy.wululuaplan.service.CourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author Mr.Hongtao
 * @since 2024-02-28
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {
    @Resource
    private AttendanceMapper attendanceMapper;

    @Resource
    private CourseScheduleMapper courseScheduleMapper;

    @Override
    public List<Course> findCourseList() {
        return baseMapper.selectList(Wrappers.<Course>lambdaQuery()
                .orderByDesc(Course::getUpdateTime));
    }

    @Override
    @Transactional
    public void create(CourseForm form) {
        Course exist = baseMapper.selectOne(Wrappers.<Course>lambdaQuery()
                .eq(Course::getCourseName, form.getCourseName())
                .eq(Course::getSession, form.getSession()));
        if (exist != null) {
            throw new CustomException(-2, "新建失败！ 当前已存在：" + form.getCourseName() + " - " + form.getSession() + " 期课程");
        }
        Course course = new Course();
        BeanUtils.copyProperties(form, course);
        baseMapper.insert(course);
    }

    /**
     * 编辑课程
     * <p>根据传入的课程id判断是否存在该课程</p>
     * <p>不存在抛出异常</p>
     * <p>存在，则继续查询是否存在同名且同期的课程</p>
     * <p>不存在才可以编辑成功</p>
     * @param form 编辑表单
     */
    @Override
    @Transactional
    public void edit(CourseForm form) {
        Course course = baseMapper.selectById(form.getId());
        if (course == null) {
            throw new CustomException(-10000, "当前课程不存在");
        }
        Course exist = baseMapper.selectOne(Wrappers.<Course>lambdaQuery()
                .eq(Course::getCourseName, form.getCourseName())
                .eq(Course::getSession, form.getSession()));
        if (exist != null) {
            throw new CustomException(-2, "修改失败！当前已存在: " + form.getCourseName() + " - " + form.getSession() + " 期课程");
        }
        BeanUtils.copyProperties(form, course);
        baseMapper.updateById(course);
    }

    /**
     * 删除课程
     * 如果当前课程已正常上过课（即存在考勤学员的），不能删除。
     * @param id 课程id
     */
    @Override
    @Transactional
    public void delete(Integer id) {

        Long count = attendanceMapper.selectCourseHasAttendance(id);
        if (count > 0) {
            throw new CustomException(-3, "当前课程已考勤过，不能删除");
        }
        baseMapper.deleteById(id);
    }

    @Override
    public Boolean coursePlanStatus(Integer courseId) {
        Long aLong = courseScheduleMapper.selectCount(Wrappers.<CourseSchedule>lambdaQuery()
                .eq(CourseSchedule::getCourseId, courseId));

        return aLong > 0;
    }
}
